﻿Imports lbGlobalcom
Public Class fRegistroPlanes
    Dim iTipoDoc As cTipoDocumento
    Dim iFP As cFormasPago
    Dim daPlan As lbGlobalcom.dsMovimientoTableAdapters.MOV_PLANTableAdapter
    Dim daPrepack As lbGlobalcom.dsInventariosTableAdapters.INV_PREPACKTableAdapter
    Dim daPlanDtalle As lbGlobalcom.dsMovimientoTableAdapters.MOV_PLANDETALLETableAdapter
    Dim daInve As lbGlobalcom.dsInventariosTableAdapters.INV_DETALLETableAdapter
    Dim iTerce As cTercero
    Dim cProd As lbGlobalcom.cProducto = Nothing
    Dim cDetLis As lbGlobalcom.cDetalleListaPrecios
    Dim cListaP As lbGlobalcom.cEncaListaPrecios
    Dim lEliminaDoc As Boolean = False
    Dim mensajeValidacion As String = ""
    Private Sub CargaTipoDoc(ByVal TipoDoc As String)
        Try
            iTipoDoc = New cTipoDocumento(TipoDoc)
            If iTipoDoc Is Nothing Then
                MessageBox.Show("El tipo de documento seleccionado no esta registrado", "Error en tipo de documento", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Else
                NUMEROTextBox.Text = lbGlobalcom.Consec.GetNumeroActual(TipoDoc, iTipoDoc.DocEs)
                IDBODEGAORIGENTextBox.Text = iTipoDoc.BodegaDe
                IDCCOSTOTextBox.Text = iTipoDoc.idCcosto
                IDSEDETextBox.Text = iTipoDoc.idSede
                IDCAJATextBox.Text = iTipoDoc.idCaja
                IDLISTATextBox.Text = iTipoDoc.idLista
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscaDocumento_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaDocumento.Click
        Try
            Dim fLDoc As New fListaTipoDocs
            fLDoc.DocEs = "Planes"
            fLDoc.SubClase = "PLA"
            If fLDoc.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDDOCUMENTOTextBox.Text = fLDoc.idTipoDoc
                CargaTipoDoc(fLDoc.idTipoDoc)
                MOV_PLANBindingSource.EndEdit()
                'SalvaCambios(False)
            End If

        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscaSede_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaSede.Click
        Try
            Dim fLS As New fListaSedes
            If fLS.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDSEDETextBox.Text = fLS.Sede
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscaCcosto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaCcosto.Click
        Try
            Dim fCC As New fListaCCostos
            If fCC.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDCCOSTOTextBox.Text = fCC.CCosto
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscaLista_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaLista.Click
        Try
            Dim fLP As New fListaPrecios
            If fLP.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDLISTATextBox.Text = fLP.Lista
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    'Private Sub ActualizaFormaPago(ByVal FP As String)
    '    iFP = New cFormasPago(FP)
    '    Try
    '        FECHAVENCIMIENTODateTimePicker.Value = iFP.GetFechaVence(FECHADOCUMENTODateTimePicker.Value)
    '    Catch ex As Exception
    '        lbGlobalcom.cError.SalvaError(ex, "Samuel")
    '        MessageBox.Show(ex.Message)
    '    End Try
    'End Sub

    Private Sub ActualizaDatosTercero()
        Try
            iTerce = New cTercero(IDTERCEROTextBox.Text, "Cli")
            If iTerce IsNot Nothing Then
                txtNombreTercero.Text = iTerce.NombreCompleto
                TXTDIRECCION.Text = iTerce.Direccion
                Dim daMuni As New lbGlobalcom.dsGlobalTableAdapters.GLO_MUNICIPIOSTableAdapter
                TXTCIUDAD.Text = iTerce.iMunicipios.Municipio
                If iTipoDoc.DocEs = eTipoMovimi.Venta Or iTipoDoc.DocEs = eTipoMovimi.Planes Then
                    If iTerce.iClientes IsNot Nothing Then
                        If IDCANALTextBox.Text.Trim.Length = 0 Then _
                            IDCANALTextBox.Text = iTerce.iClientes.idCanal
                        IDFORMAPAGOTextBox.Text = iTerce.iClientes.idFormaPago
                        If iTerce.iClientes.idVendedor.Length > 0 Then
                            IDVENDEDORTextBox.Text = iTerce.iClientes.idVendedor
                        End If
                        If iTerce.iClientes.idListaGlobal IsNot Nothing AndAlso iTerce.iClientes.idListaGlobal.Trim.Length = 0 Then _
                            IDLISTATextBox.Text = iTerce.iClientes.idListaGlobal
                        If IDFORMAPAGOTextBox.Text.Trim.Length = 0 Then
                            iTerce.iClientes.SeekClienteBySucursal(SUCURSALTextBox.Text)
                            'ActualizaFormaPago(iTerce.iClientes.idFormaPago)
                        End If
                        If Not iTerce.iClientes.BodegaConsignacion Is DBNull.Value AndAlso iTerce.iClientes.BodegaConsignacion.Trim.Length > 0 Then
                            If Me.IDBODEGAORIGENTextBox.Text.Length = 0 Then
                                Me.IDBODEGAORIGENTextBox.Text = iTerce.iClientes.BodegaConsignacion
                            End If

                            Me.IDBODEGAORIGENTextBox.Text = iTerce.iClientes.BodegaConsignacion
                        End If
                    Else
                        MessageBox.Show("Cliente no existe verifique e intente de nuevo", "Cliente No Existe", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                    End If
                End If
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscaTercero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaTercero.Click
        Try
            Dim fBT As New fBuscaTercero
            fBT.TipoTercero = "Cliente"
            'daProv = New lbGlobalcom.dsTerceroTableAdapters.TER_PROVEEDORESTableAdapter

            If fBT.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDTERCEROTextBox.Text = fBT.midTercero
                SUCURSALTextBox.Text = fBT.mSucursal
                iTerce = New cTercero(IDTERCEROTextBox.Text, "Cli")
                iTerce.iClientes.SeekClienteBySucursal(SUCURSALTextBox.Text)
                ActualizaDatosTercero()
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscaFormapago_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaFormapago.Click
        Try
            Dim fLisFP As New fListaFormasPago
            If fLisFP.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDFORMAPAGOTextBox.Text = fLisFP.FP
                'ActualizaFormaPago(fLisFP.FP)
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub fRegistroPlanes_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            daPlan = New lbGlobalcom.dsMovimientoTableAdapters.MOV_PLANTableAdapter
            daPlanDtalle = New lbGlobalcom.dsMovimientoTableAdapters.MOV_PLANDETALLETableAdapter
            daInve = New lbGlobalcom.dsInventariosTableAdapters.INV_DETALLETableAdapter
            daPrepack = New lbGlobalcom.dsInventariosTableAdapters.INV_PREPACKTableAdapter
            DsMovimiento.MOV_PLAN.Columns("FECHADOCUMENTO").DefaultValue = Date.Today
            DsMovimiento.MOV_PLANDETALLE.Columns("ACTIVO").DefaultValue = "A"
            DsMovimiento.MOV_PLANDETALLE.Columns("VALORBASEIMEI").DefaultValue = 0
            DsMovimiento.MOV_PLANDETALLE.Columns("VALORIVAIMEI").DefaultValue = 0
            DsMovimiento.MOV_PLANDETALLE.Columns("VALORBASEICCID").DefaultValue = 0
            DsMovimiento.MOV_PLANDETALLE.Columns("VALORIVAICCID").DefaultValue = 0
            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, False)
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub SalvaCambios(ByVal Msj As Boolean)
        Try
            IDDOCUMENTOTextBox.Enabled = False
            NUMEROTextBox.Enabled = False
            btnBuscaDocumento.Enabled = False
            If Msj Then _
                Validate()
            MOV_PLANBindingSource.EndEdit()
            MOV_PLANDETALLEBindingSource.EndEdit()
            If lEliminaDoc Then
                daPlanDtalle.Update(DsMovimiento.MOV_PLANDETALLE)
                daPlan.Update(DsMovimiento.MOV_PLAN)
                lEliminaDoc = False
            Else
                daPlan.Update(DsMovimiento.MOV_PLAN)
                daPlanDtalle.Update(DsMovimiento.MOV_PLANDETALLE)
            End If
            If Msj Then _
                MessageBox.Show("Se salvarón cambios con exito", "Salvar Cambios", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        Try
            IDDOCUMENTOTextBox.Enabled = True
            NUMEROTextBox.Enabled = True
            btnBuscaDocumento.Enabled = True
            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
            If DsMovimiento.MOV_PLAN.GetChanges IsNot Nothing Or DsMovimiento.MOV_PLANDETALLE.GetChanges IsNot Nothing Then
                If MessageBox.Show("Hay cambios pendientes por salvar, desea salvarlos", "Cambios pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    SalvaCambios(True)
                End If
            End If
            MOV_PLANBindingSource.CancelEdit()
            DsMovimiento.MOV_PLANDETALLE.Rows.Clear()
            DsMovimiento.MOV_PLAN.Rows.Clear()
            MOV_PLANBindingSource.AddNew()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub MOV_PLANBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MOV_PLANBindingNavigatorSaveItem.Click
        Try
            SalvaCambios(True)
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        Try

            If MessageBox.Show("Esta seguro que desea eliminar este documento", "Elimina Documento", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                MOV_PLANBindingSource.RemoveCurrent()
                lEliminaDoc = True
                SalvaCambios(False)
                MessageBox.Show("Se elimino documento con exito", "Elimino Documento", MessageBoxButtons.OK, MessageBoxIcon.Information)
                lbGlobalcom.cGeneral.ActivaControles(Me.Controls, False)
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub MOV_PLANDETALLEDataGridView_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles MOV_PLANDETALLEDataGridView.CellBeginEdit
        If NUMEROTextBox.Text <> "" AndAlso IDDOCUMENTOTextBox.Text <> "" Then
            MOV_PLANDETALLEDataGridView.Rows(e.RowIndex).Cells("NUMEROLINEA").Value = cGeneral.GetNumeroLinea(MOV_PLANDETALLEDataGridView, e.RowIndex, "NUMEROLINEA")
        End If
        'If e.RowIndex = 0 Then
        '    MOV_PLANDETALLEDataGridView.Rows(e.RowIndex).Cells("NUMEROLINEA").Value = 1
        'Else
        '    MOV_PLANDETALLEDataGridView.Rows(e.RowIndex).Cells("NUMEROLINEA").Value = MOV_PLANDETALLEDataGridView.Rows(e.RowIndex - 1).Cells("NUMEROLINEA").Value + 1
        'End If
    End Sub

    Private Sub MOV_PLANDETALLEDataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles MOV_PLANDETALLEDataGridView.CellMouseDoubleClick
        Try
            Select Case MOV_PLANDETALLEDataGridView.Columns(e.ColumnIndex).Name
                Case "IDPRODUCTOIMEI"
                    If IDDOCUMENTOTextBox.Text.Length > 0 And NUMEROTextBox.Text.Length > 0 Then
                        'If MOV_PLANDETALLEDataGridView.CurrentCellAddress.X = 5 Then
                        Dim fLProd As New fListaProductos
                        fLProd.ListaPrecios = IDLISTATextBox.Text
                        If fLProd.ShowDialog = Windows.Forms.DialogResult.OK Then
                            MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("IDPRODUCTOIMEI").Value = fLProd.idArticulo
                            MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("ACTIVO").Value = "A"
                            cProd = New cProducto(fLProd.idArticulo)
                            cDetLis = New cDetalleListaPrecios(IDLISTATextBox.Text, fLProd.idArticulo)
                            MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("VALORBASEIMEI").Value = cDetLis.PrecioVenta
                            MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("VALORIVAIMEI").Value = cDetLis.Iva
                        End If
                        'End If
                    End If
                Case "IDPRODUCTOICCID"
                    If IDDOCUMENTOTextBox.Text.Length > 0 And NUMEROTextBox.Text.Length > 0 Then
                        'If MOV_PLANDETALLEDataGridView.CurrentCellAddress.X = 5 Then
                        Dim fLProd As New fListaProductos
                        fLProd.ListaPrecios = IDLISTATextBox.Text
                        If fLProd.ShowDialog = Windows.Forms.DialogResult.OK Then
                            MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("IDPRODUCTOICCID").Value = fLProd.idArticulo
                            MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("ACTIVO").Value = "A"
                            cProd = New cProducto(fLProd.idArticulo)
                            cDetLis = New cDetalleListaPrecios(IDLISTATextBox.Text, fLProd.idArticulo)
                            MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("VALORBASEICCID").Value = cDetLis.PrecioVenta
                            MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("VALORIVAICCID").Value = cDetLis.Iva
                        End If
                        'End If
                    End If

                Case "IDPLAN"
                    If IDDOCUMENTOTextBox.Text.Length > 0 And NUMEROTextBox.Text.Length > 0 Then
                        'If MOV_PLANDETALLEDataGridView.CurrentCellAddress.X = 3 Then
                        Dim flPlan As New fListaPlanes
                        If flPlan.ShowDialog = Windows.Forms.DialogResult.OK Then
                            MOV_PLANDETALLEDataGridView.BeginEdit(True)
                            MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("IDPLAN").Value = flPlan.mPlan
                            MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("VALOR").Value = flPlan.mValorBase + flPlan.mValorIva
                            MOV_PLANDETALLEBindingSource.EndEdit()
                        End If
                        'End If
                    End If
                Case "IMEI"
                    Dim flPrep As New fListaPrepack
                    'flPrep.mIdPrepack = MOV_MOVIMIENTODETALLEDataGridView.Rows(MOV_MOVIMIENTODETALLEDataGridView.CurrentRow.Index).Cells("IDPREPACK").Value
                    flPrep.mBodega = IDBODEGAORIGENTextBox.Text
                    flPrep.mProducto = MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("IDPRODUCTOIMEI").Value
                    flPrep.EsLista = True
                    cProd = New cProducto(MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("IDPRODUCTOIMEI").Value)
                    flPrep.mTipoSubProd = cProd.TipoSubProd
                    If flPrep.ShowDialog = Windows.Forms.DialogResult.OK Then
                        Dim dPPackSel As cPrepacksSelect = flPrep.ListaPPack.Item(0)
                        Dim pendienteFactura As Integer = cFacturacion.equipoPendientePorFacturar(dPPackSel.NumeroRegP)
                        If pendienteFactura = 0 Then
                            Dim drInvePadre As dsInventarios.INV_DETALLERow = daInve.GetDataByDatosImeiIccid(dPPackSel.NumeroRegP).Rows(0)
                            MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("IMEI").Value = dPPackSel.NumeroRegP
                            MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("IDPRODUCTOIMEI").Value = drInvePadre("IDPRODUCTO")
                            Dim iPrep As cPrepack
                            iPrep = New cPrepack(drInvePadre.TIPOSUBPROD, dPPackSel.NumeroRegP, True)
                            If iPrep.EsPrepack = True Then
                                MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("IDPRODUCTOICCID").Value = iPrep.IdProductoH
                                cDetLis = New cDetalleListaPrecios(IDLISTATextBox.Text, iPrep.IdProductoH)
                                MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("VALORBASEICCID").Value = cDetLis.PrecioConDto
                                MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("VALORIVAICCID").Value = cDetLis.Iva
                                MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("ICCID").Value = iPrep.NumeroRegsitroH
                            End If
                            MOV_PLANDETALLEBindingSource.EndEdit()
                        Else
                            MessageBox.Show("El equipo ya se encuentra pendiente por facturar", "PendienteFactura", MessageBoxButtons.OK)
                        End If
                    End If
            End Select
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscaVendedor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaVendedor.Click
        Dim fVen As New fBuscaTercero
        fVen.TipoTercero = "Vendedor"
        If fVen.ShowDialog = Windows.Forms.DialogResult.OK Then
            IDVENDEDORTextBox.Text = fVen.midTercero
        End If
    End Sub

    Private Sub btnBuscaBodega_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaBodega.Click
        Try
            Dim fBusBod As New fListaBodegas
            fBusBod.idSede = IDSEDETextBox.Text
            If fBusBod.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDBODEGAORIGENTextBox.Text = fBusBod.Bodega
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscaCaja_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaCaja.Click
        Try
            Dim flCaja As New fListaCajas
            flCaja.idSede = IDSEDETextBox.Text
            If flCaja.ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.IDCAJATextBox.Text = flCaja.caja
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscaCanal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaCanal.Click
        Try
            Dim fLC As New fListaCanales
            If fLC.ShowDialog = Windows.Forms.DialogResult.OK Then
                IDCANALTextBox.Text = fLC.Canal
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub MOV_PLANDETALLEDataGridView_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles MOV_PLANDETALLEDataGridView.CellValidated
        If MOV_PLANDETALLEDataGridView.IsCurrentCellInEditMode Then
            Select Case MOV_PLANDETALLEDataGridView.Columns(e.ColumnIndex).Name
                Case "IMEI"
                    If MOV_PLANDETALLEDataGridView.CurrentRow.Cells("IMEI").Value.ToString.Length > 0 Then
                        Dim drInvePadre As dsInventarios.INV_DETALLERow = daInve.GetDataByDatosImeiIccid(MOV_PLANDETALLEDataGridView.CurrentRow.Cells("IMEI").Value).Rows(0)
                        MOV_PLANDETALLEDataGridView.CurrentRow.Cells("IDPRODUCTOIMEI").Value = drInvePadre("IDPRODUCTO")
                        Dim iPrep As cPrepack
                        iPrep = New cPrepack(drInvePadre.TIPOSUBPROD, MOV_PLANDETALLEDataGridView.CurrentRow.Cells("IMEI").Value, True)
                        If iPrep.EsPrepack = True Then
                            MOV_PLANDETALLEDataGridView.CurrentRow.Cells("IDPRODUCTOICCID").Value = iPrep.IdProductoH
                            cDetLis = New cDetalleListaPrecios(IDLISTATextBox.Text, iPrep.IdProductoH)
                            MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALORBASEICCID").Value = cDetLis.PrecioConDto
                            MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALORIVAICCID").Value = cDetLis.Iva
                            MOV_PLANDETALLEDataGridView.CurrentRow.Cells("ICCID").Value = iPrep.NumeroRegsitroH
                        End If
                        SalvaCambios(False)
                    End If
                Case "ICCID"
                    If MOV_PLANDETALLEDataGridView.CurrentRow.Cells("ICCID").Value.ToString.Length > 0 Then
                        Dim drInvePadre As dsInventarios.INV_DETALLERow = daInve.GetDataByDatosImeiIccid(MOV_PLANDETALLEDataGridView.CurrentRow.Cells("ICCID").Value).Rows(0)
                        MOV_PLANDETALLEDataGridView.CurrentRow.Cells("IDPRODUCTOICCID").Value = drInvePadre("IDPRODUCTO")
                        SalvaCambios(False)
                    End If
                Case "IDPRODUCTO"
                    SalvaCambios(False)
                Case "IDPLAN"
                    SalvaCambios(False)
            End Select
        End If
    End Sub

    Private Function validaImeiIccid(ByVal numeroRegistro As String) As Integer
        Dim existe As Integer = 0
        Try
            If MOV_PLANDETALLEDataGridView.Rows.Count > 0 Then
                For Each drDes As dsMovimiento.MOV_PLANDETALLERow In DsMovimiento.MOV_PLANDETALLE.Rows
                    If drDes("IMEI") = numeroRegistro Or drDes("ICCID") = numeroRegistro Then
                        existe = 1
                    End If
                Next
            End If
            Return existe
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Function

    Private Sub MOV_PLANDETALLEDataGridView_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles MOV_PLANDETALLEDataGridView.CellValidating
        Try
            If MOV_PLANDETALLEDataGridView.IsCurrentCellInEditMode AndAlso e.FormattedValue IsNot Nothing AndAlso e.FormattedValue <> "" Then
                Select Case MOV_PLANDETALLEDataGridView.Columns(e.ColumnIndex).Name
                    Case "ANTICIPO"
                        If e.FormattedValue > (MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALOR").Value + MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALORBASEIMEI").Value + MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALORIVAIMEI").Value + MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALORBASEICCID").Value + MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALORIVAICCID").Value) Then
                            MessageBox.Show("Anticipo mayor del valor del plan", "Anticipo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                            e.Cancel = True
                        End If
                    Case "IMEI", "ICCID"
                        If e.FormattedValue.ToString.Length > 0 Then
                            If daInve.GetImeiIccidValido(e.FormattedValue) = 1 Then
                                Dim drInvePadre As dsInventarios.INV_DETALLERow = daInve.GetDataByDatosImeiIccid(e.FormattedValue).Rows(0)
                                If daPrepack.GetImeiAndIccidValido(drInvePadre("IDPRODUCTO"), drInvePadre("NUMEROREGISTRO")) = 0 Then
                                    MessageBox.Show("No esta creado como Prepack", "Prepack", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                                    e.Cancel = True
                                End If
                            Else
                                MessageBox.Show("El numero de registro no es valido", "NumeroRegistro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                                e.Cancel = True
                            End If
                        End If
                    Case "VALORBASEIMEI"
                        If e.FormattedValue < 0 Then
                            MessageBox.Show("Debe ingresar un valor mayor o igual a cero", "VALORINVALIDO", MessageBoxButtons.OK)
                            e.Cancel = True
                        End If
                        If (MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALOR").Value + e.FormattedValue + MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALORIVAIMEI").Value + MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALORBASEICCID").Value + MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALORIVAICCID").Value) < MOV_PLANDETALLEDataGridView.CurrentRow.Cells("ANTICIPO").Value And MOV_PLANDETALLEDataGridView.CurrentRow.Cells("ANTICIPO").Value > 0 Then
                            MessageBox.Show("Debe ingresar un valor menor en el anticipo", "VALORINVALIDO", MessageBoxButtons.OK)
                            e.Cancel = True
                        End If
                    Case "VALORIVAIMEI"
                        If e.FormattedValue < 0 Then
                            MessageBox.Show("Debe ingresar un valor mayor o igual a cero", "VALORINVALIDO", MessageBoxButtons.OK)
                            e.Cancel = True
                        End If
                        If (MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALOR").Value + MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALORBASEIMEI").Value + e.FormattedValue + MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALORBASEICCID").Value + MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALORIVAICCID").Value) < MOV_PLANDETALLEDataGridView.CurrentRow.Cells("ANTICIPO").Value And MOV_PLANDETALLEDataGridView.CurrentRow.Cells("ANTICIPO").Value > 0 Then
                            MessageBox.Show("Debe ingresar un valor menor en el anticipo", "VALORINVALIDO", MessageBoxButtons.OK)
                            e.Cancel = True
                        End If
                    Case "VALORBASEICCID"
                        If e.FormattedValue < 0 Then
                            MessageBox.Show("Debe ingresar un valor mayor o igual a cero", "VALORINVALIDO", MessageBoxButtons.OK)
                            e.Cancel = True
                        End If
                        If (MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALOR").Value + MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALORBASEIMEI").Value + MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALORIVAIMEI").Value + e.FormattedValue + MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALORIVAICCID").Value) < MOV_PLANDETALLEDataGridView.CurrentRow.Cells("ANTICIPO").Value And MOV_PLANDETALLEDataGridView.CurrentRow.Cells("ANTICIPO").Value > 0 Then
                            MessageBox.Show("Debe ingresar un valor menor en el anticipo", "VALORINVALIDO", MessageBoxButtons.OK)
                            e.Cancel = True
                        End If
                    Case "VALORIVAICCID"
                        If e.FormattedValue < 0 Then
                            MessageBox.Show("Debe ingresar un valor mayor o igual a cero", "VALORINVALIDO", MessageBoxButtons.OK)
                            e.Cancel = True
                        End If
                        If (MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALOR").Value + MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALORBASEIMEI").Value + MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALORIVAIMEI").Value + MOV_PLANDETALLEDataGridView.CurrentRow.Cells("VALORBASEICCID").Value + e.FormattedValue) < MOV_PLANDETALLEDataGridView.CurrentRow.Cells("ANTICIPO").Value And MOV_PLANDETALLEDataGridView.CurrentRow.Cells("ANTICIPO").Value > 0 Then
                            MessageBox.Show("Debe ingresar un valor menor en el anticipo", "VALORINVALIDO", MessageBoxButtons.OK)
                            e.Cancel = True
                        End If
                End Select
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            'stMovimi.Text = ""
            e.Cancel = True
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub MOV_PLANDETALLEDataGridView_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles MOV_PLANDETALLEDataGridView.DataError
        e.Cancel = True
        lbGlobalcom.cError.SalvaError(e.Exception, "Henry")
        MessageBox.Show(e.Exception.Message)
    End Sub


    Private Sub MOV_PLANDETALLEDataGridView_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MOV_PLANDETALLEDataGridView.GotFocus
        Dim cVal As New cValidaciones(DsMovimiento)
        Try
            If IDDOCUMENTOTextBox.Text.Length > 0 And NUMEROTextBox.Text.Length > 0 Then
                MOV_PLANBindingSource.EndEdit()
                mensajeValidacion = cVal.ValidaRegistroPlan(iTipoDoc.DocEs)
                If mensajeValidacion <> "" Then
                    IDSEDETextBox.Focus()
                    MessageBox.Show(mensajeValidacion, "validaciones", MessageBoxButtons.OK)
                Else
                    If DsMovimiento.MOV_PLAN.Rows(MOV_PLANBindingSource.Position).RowState = DataRowState.Added Then
                        NUMEROTextBox.Text = lbGlobalcom.Consec.GetNumeroActual(IDDOCUMENTOTextBox.Text, iTipoDoc.DocEs)
                        SalvaCambios(False)
                    End If
                End If
            Else
                IDSEDETextBox.Focus()
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub MOV_PLANDETALLEDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MOV_PLANDETALLEDataGridView.KeyDown
        Try
            If e.Control Then
                Select Case e.KeyCode
                    Case Keys.B
                        If IDDOCUMENTOTextBox.Text.Length > 0 And NUMEROTextBox.Text.Length > 0 Then
                            If MOV_PLANDETALLEDataGridView.Columns(MOV_PLANDETALLEDataGridView.CurrentCell.ColumnIndex).Name = "IDPRODUCTOIMEI" Then
                                Dim fLProd As New fListaProductos
                                fLProd.ListaPrecios = IDLISTATextBox.Text
                                If fLProd.ShowDialog = Windows.Forms.DialogResult.OK Then
                                    MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("IDPRODUCTOIMEI").Value = fLProd.idArticulo
                                    cProd = New cProducto(fLProd.idArticulo)
                                    cDetLis = New cDetalleListaPrecios(IDLISTATextBox.Text, fLProd.idArticulo)
                                    MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("VALORBASEIMEI").Value = cDetLis.PrecioVenta
                                    MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("VALORIVAIMEI").Value = cDetLis.Iva
                                End If
                                'cDetLis = cListaP.DetalleLista(fLProd.idArticulo, IDCANALTextBox.Text)
                                'ActualizaDatosProd(MOV_PLANDETALLEDataGridView.CurrentCellAddress.Y)
                                'stMovimi.Text = "PRODUCTO: " & cDetLis.iProducto.Descripcion & " UNIDAD: " & cDetLis.iProducto.Unidad & " CONTENIDO: " & cDetLis.iProducto.Contenido
                                'MOV_MOVIMIENTODETALLEDataGridView.BeginEdit(True)
                            End If
                        End If
                        If IDDOCUMENTOTextBox.Text.Length > 0 And NUMEROTextBox.Text.Length > 0 Then
                            If MOV_PLANDETALLEDataGridView.Columns(MOV_PLANDETALLEDataGridView.CurrentCell.ColumnIndex).Name = "IDPRODUCTOICCID" Then
                                Dim fLProd As New fListaProductos
                                fLProd.ListaPrecios = IDLISTATextBox.Text
                                If fLProd.ShowDialog = Windows.Forms.DialogResult.OK Then
                                    MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("IDPRODUCTOICCID").Value = fLProd.idArticulo
                                    cProd = New cProducto(fLProd.idArticulo)
                                    cDetLis = New cDetalleListaPrecios(IDLISTATextBox.Text, fLProd.idArticulo)
                                    MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("VALORBASEICCID").Value = cDetLis.PrecioVenta
                                    MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("VALORIVAICCID").Value = cDetLis.Iva
                                End If

                                'cDetLis = cListaP.DetalleLista(fLProd.idArticulo, IDCANALTextBox.Text)
                                'ActualizaDatosProd(MOV_PLANDETALLEDataGridView.CurrentCellAddress.Y)
                                'stMovimi.Text = "PRODUCTO: " & cDetLis.iProducto.Descripcion & " UNIDAD: " & cDetLis.iProducto.Unidad & " CONTENIDO: " & cDetLis.iProducto.Contenido
                                'MOV_MOVIMIENTODETALLEDataGridView.BeginEdit(True)
                            End If
                        End If
                        If IDDOCUMENTOTextBox.Text.Length > 0 And NUMEROTextBox.Text.Length > 0 Then
                            If MOV_PLANDETALLEDataGridView.Columns(MOV_PLANDETALLEDataGridView.CurrentCell.ColumnIndex).Name = "IDPLAN" Then
                                Dim flPlan As New fListaPlanes
                                If flPlan.ShowDialog = Windows.Forms.DialogResult.OK Then
                                    MOV_PLANDETALLEDataGridView.BeginEdit(True)
                                    MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("IDPLAN").Value = flPlan.mPlan
                                    MOV_PLANDETALLEDataGridView.Rows(MOV_PLANDETALLEDataGridView.CurrentRow.Index).Cells("VALOR").Value = flPlan.mValorBase + flPlan.mValorIva
                                    MOV_PLANDETALLEBindingSource.EndEdit()
                                End If
                            End If
                        End If
                End Select
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub buscaPlanes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buscaPlanes.Click
        Try
            Dim cmov As New cMovimiento(DsMovimiento)
            Dim fListaMov As New fListaPlanesRegistrados
            fListaMov.SubClase = "PLA"
            If fListaMov.ShowDialog = Windows.Forms.DialogResult.OK Then
                If DsMovimiento.MOV_PLAN.GetChanges IsNot Nothing Or DsMovimiento.MOV_PLANDETALLE.GetChanges IsNot Nothing Then
                    If MessageBox.Show("Hay cambios pendientes por salvar, desae salvarlos", "Cambios Pendientes", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                        SalvaCambios(False)
                    End If
                End If
                DsMovimiento.MOV_PLANDETALLE.Rows.Clear()
                DsMovimiento.MOV_PLAN.Rows.Clear()
                daPlan.FillByIdDocumentoNumero(DsMovimiento.MOV_PLAN, fListaMov.mTipoDoc, fListaMov.mNumeroDoc)
                daPlanDtalle.FillByIdDocumentoNumero(DsMovimiento.MOV_PLANDETALLE, fListaMov.mTipoDoc, fListaMov.mNumeroDoc)
                If CustomPrincipal.IsInRole("ADMINGC") = False And cmov.buscarCerrado(IDDOCUMENTOTextBox.Text, NUMEROTextBox.Text) = "S" Then
                    lbGlobalcom.cGeneral.bloquearDocumento(Me.Controls, False)
                Else
                    lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
                End If
                IDDOCUMENTOTextBox.Enabled = False
                NUMEROTextBox.Enabled = False
                btnBuscaDocumento.Enabled = False
                iTipoDoc = New cTipoDocumento(IDDOCUMENTOTextBox.Text)
                ActualizaDatosTercero()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
        End Try
    End Sub

    Private Sub IDLISTATextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles IDLISTATextBox.TextChanged
        Try
            If IDLISTATextBox.Text.Length > 0 Then _
                cListaP = New cEncaListaPrecios(IDLISTATextBox.Text)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnEliminaMovi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminaMovi.Click
        Try
            If MOV_PLANDETALLEDataGridView.RowCount > 0 Then
                MOV_PLANDETALLEBindingSource.RemoveCurrent()
                lEliminaDoc = True
                SalvaCambios(False)
                MessageBox.Show("Registro eliminado con exito", "Elimina", MessageBoxButtons.OK)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub
End Class